package com.fangngng.pushall.exception;

import com.fangngng.pushall.constant.ErrorCode;
import com.fangngng.pushall.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author fei.fang
 * @date 2021/4/2
 **/
@RestControllerAdvice
@Slf4j
public class GlobeExceptionAdvice {

    @ExceptionHandler(BadCredentialsException.class)
    public Result<Object> badCredentialsException(BadCredentialsException e) {
        log.error("异常", e);
        return new Result<>(ErrorCode.ERROR_PERMISSION_FORBIT);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result<Object> accessDeniedException(AccessDeniedException e) {
        log.error("权限异常", e);
        return new Result<>(ErrorCode.ERROR_NEED_LOGIN);
    }

    @ExceptionHandler(BusinessException.class)
    public Result<Object> sendErrorResponseSystem(BusinessException e){
        log.error("业务异常", e);
        return new Result<>(e.getErrorCode(), e.getErrorMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result<Object> sendErrorResponseSystem(Exception e){
        log.error("异常", e);
        return new Result<>(Result.CODE_FAIL, e.getMessage());
    }
}
